package cn.tedu.micro.demo.all.service;

import cn.tedu.micro.demo.all.dao.repository.CartRepository;
import cn.tedu.micro.demo.all.dao.repository.CartRepositoryImpl;
import cn.tedu.micro.demo.all.dao.repository.OrderRepository;
import cn.tedu.micro.demo.all.dao.repository.StockRepository;
import cn.tedu.micro.demo.pojo.po.OrderPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private StockRepository stockRepository;
    //update stock_tbl set stock=stock-#{count} where product_code=#{productCode}
    @Autowired
    private CartRepository cartRepository;
    //delete from cart_tbl where user_id=#{userId} and product_code=#{productCode}
    @Autowired
    private OrderRepository orderRepository;
    //insert into order_tbl(user_id,product_code,count) values(#{userId},#{productCode},#{count})

    @Override
    public void submitOrder(String userId, String productCode, Integer count) {
        //1.减库存
        stockRepository.updateStock(productCode,count);
        //2.增订单
        OrderPO po=new OrderPO();
        po.setUserId(userId);
        po.setCount(count);
        po.setProductCode(productCode);
        po.setOrderSn(System.currentTimeMillis()+"");
        orderRepository.save(po);
        //3.删除购物车
        cartRepository.deleteCart(userId,productCode);
    }
}
